/*
 * @(#)DrawVertex.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library itfP free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library itfP distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.editor.spec;

import helloworld.Interface;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;

import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import fr.ema.arch3d.editor.port.MyPort;
import fr.ema.arch3d.xoadl.interfaceADL.InterfaceADL;
import fr.ema.arch3d.xoadl.specification.ComponentRoleADL;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class DrawVertex {

    /** Field description */
    static int portRNo=0;

    /** Field description */
    static List port_client_list;

    /** Field description */
    static List port_server_list;

    /** Field description */
    static int portPNo=0;

    public SpecVertex drawVertext(ComponentRoleADL cr, int i) throws NoSuchInterfaceException {
        SpecVertex cell = createVertex(cr, null, false, null, i);

        // System.out.println("))"+cmpC.cmpType.name);
        return cell;
    }

    public static SpecVertex createVertex(ComponentRoleADL cr, Color bg, boolean raised, String viewClass, int i)
            throws NoSuchInterfaceException {
        SpecVertex cell = new SpecVertex(cr.getName());

        // modify font
        Font f  = GraphConstants.getFont(cell.getAttributes());
        Font ff = new Font("ff", Font.ITALIC, f.getSize());

        GraphConstants.setFont(cell.getAttributes(), ff);
        cell.setVertexName(cr.getName());

        // set the view class (indirection for the renderer and the editor)
        if (viewClass != null) {
            GPCellViewFactory.setViewClass(cell.getAttributes(), viewClass);
        }

        // Set fill color
        if (bg != null) {
            GraphConstants.setGradientColor(cell.getAttributes(), bg);
            GraphConstants.setOpaque(cell.getAttributes(), true);
        }

        // Set raised border
        if (raised) {
            GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createRaisedBevelBorder());
        } else {

            // Set black border
            GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
        }

        // Add a Floating Port
        addPorts(cell, cr);

        // Set bounds
        if (portPNo > portRNo) {
            GraphConstants.setBounds(cell.getAttributes(),
                                     new Rectangle2D.Double(20 + i * 200, 20, cr.getName().length() * 8,
                                         portPNo * 20 + 40));
        } else {
            GraphConstants.setBounds(cell.getAttributes(),
                                     new Rectangle2D.Double(20 + i * 200, 20, cr.getName().length() * 8,
                                         portRNo * 20 + 40));
        }

        return cell;
    }

    public static DefaultGraphCell addPorts(DefaultGraphCell cell, ComponentRoleADL cr) {
        port_client_list = new ArrayList();
        portRNo             = 0;
        port_server_list = new ArrayList();
        portPNo=0;
        Interface[] clientItfs;
        Interface[] serverItfs;
        List<InterfaceADL> interfaces=cr.getInterface();
  
        for (int i = 0; i < interfaces.size(); i++) {
            if ((interfaces.get(i)).getRole().equals("required")) {
                portRNo++;
                MyPort p = new MyPort();
                p.setPortName(interfaces.get(i).getName());
                p.setPortType("client");
                port_client_list.add(p);
                cell.add(p);
            } else if (interfaces.get(i).getRole().equals("provided")) {
                portPNo++;
                MyPort p = new MyPort();
                p.setPortName(interfaces.get(i).getName());
                p.setPortType("server");
                port_server_list.add(p);
                cell.add(p);
            }
        }

        clientItfs = new Interface[portRNo];
        serverItfs = new Interface[portPNo];

        for (int i = 0; i < port_client_list.size(); i++) {
            MyPort  p  = (MyPort) port_client_list.get(i);
            Point2D p2 = new Point2D.Double(GraphConstants.PERMILLE, GraphConstants.PERMILLE / (portRNo + 1) * (i + 1));

            GraphConstants.setOffset(p.getAttributes(), p2);
        }

        for (int i = 0; i < port_server_list.size(); i++) {
            MyPort  p  = (MyPort) port_server_list.get(i);
            Point2D p2 = new Point2D.Double(0, GraphConstants.PERMILLE / (portPNo + 1) * (i + 1));

            GraphConstants.setOffset(p.getAttributes(), p2);
        }

        return cell;
    }
}
